package org.jugile.restapi;

import java.util.Map;

import org.jugile.restapi.domain.*;
import org.jugile.util.Buffer;
import org.jugile.util.Jugile;
import org.jugile.util.Node;

public class Rest extends Jugile {
	
	private Domain domain;
	private Domain d() {
		if (domain == null) domain = Domain.getDomain("empty");
		return domain;
	}
	
	private Rest() { }
	
	public static synchronized Rest parseXmlFile(String fname) {
		Rest r = new Rest();
		r.doParse(fname);
		return r;
	}

	private Api api;
	private void doParse(String fname) {
		api = d().createApi(fname, fname);
		api.parse(parseXml(fname));
	}
	
	public Match matchUrl(String uri, String method, Map<String,String> params) {
		if (empty(uri)) return null;
		if (empty(method)) return null;
		Match m = api.match2(uri,method.toLowerCase(),params);
		return m;
	}

	public String resolveUrl(String name, String lang, String vars) {
		if (empty(name)) return null;
		if (empty(lang)) lang = "fi";
		return api.urlResolve(name, lang, vars);
	}
	
	public Node toXml() {
		return api.toXml();
	}
	
	public String dump(String lang) {
		Buffer buf = new Buffer();
		api.dump(buf,lang);
		return buf.toString();
	}
}
